<?php

namespace Biz\model\activity;

use Doraemon\model\Guide;
use \Doraemon\model\Blank;
use Doraemon\model\Photos;
use Doraemon\service\Content\AdmContent;
use Doraemon\tools\BlockType;
use Doraemon\tools\ImgUrl;
use Doraemon\model\Article as mArticle;

class PageModuleArticle extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'page_module_article';


    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;
    const ARTICLE_TYPE_HOUSE = BlockType::TYPE_ARTICLE;//整屋
    const ARTICLE_TYPE_GUIDE = BlockType::TYPE_GUIDE;//指南
    const ARTICLE_TYPE_TEXT = BlockType::TYPE_BLANK;//文章
    const ARTICLE_TYPE_NOTE = BlockType::TYPE_PHOTO;//note
    const ADMIN_ARTICLE_TYPE_MAP = [
        BlockType::TYPE_ARTICLE => '整屋',
        BlockType::TYPE_GUIDE => '指南',
        BlockType::TYPE_BLANK => '文章',
        BlockType::TYPE_IDEABOOK => '收藏夹',
        BlockType::TYPE_TOPIC_ACTIVITY => '话题',
        BlockType::TYPE_PHOTO => 'Note',
    ];

    public static function insertModuleArticle($data)
    {
        $data['article_status'] = self::STATUS_NORMAL;
        $data['addtime'] = date("Y-m-d H:i:s");
        if (self::insert($data)->rowcount()) {
            return self::id();
        } else {
            return false;
        }
    }

    public static function getModuleArticles($pageId, $moduleId)
    {
        $list = self::select('*', [
            'page_id' => $pageId,
            'module_id' => $moduleId,
            'article_status' => self::STATUS_NORMAL,
        ]);
        return empty($list) ? [] : $list;
    }

    public static function getModuleArticle($pageId, $moduleId)
    {
        return self::select('aid', [
            'page_id' => $pageId,
            'module_id' => $moduleId,
            'article_status' => self::STATUS_NORMAL,
        ]);
    }

    public static function addModuleArticleByData($insertData = [], $id = 0)
    {
        $data = [
            'page_id' => $insertData['page_id'] ?? 0,
            'article_type' => $insertData['article_type'] ?? 0,
            'aid' => $insertData['aid'] ?? '',
            'module_id' => $insertData['module_id'] ?? 0,
            'link_type' => $insertData['link_type'] ?? '',
            'link_params' => $insertData['link_params'] ?? '',
            'article_status' => self::STATUS_NORMAL,
        ];
        if ($id > 0) {
            $data['edittime'] = date('Y-m-d H:i:s');
            self::update($data, ['id' => $id]);
        } else {
            $data['addtime'] = date('Y-m-d H:i:s');
            self::insert($data);
        }
    }

    public static function addModuleArticle($pageId, $moduleId, $articleType, $articleIds)
    {
        foreach ($articleIds as $aid) {
            $data = [
                'page_id' => $pageId,
                'article_type' => $articleType,
                'module_id' => $moduleId,
                'aid' => $aid,
                'article_status' => self::STATUS_NORMAL,
                'addtime' => date('Y-m-d H:i:s')
            ];
            self::insert($data);
        }
        return true;
    }


    public static function saveModuleSort($sortInfo)
    {
        foreach ($sortInfo as $id => $sort) {
            self::update(['sort' => $sort], ['id' => $id]);
        }
    }


    public static function getArticleByModuleId($moduleId, $format = true)
    {
        $list = self::select("*",
            [
                'AND' => [
                    'module_id' => $moduleId,
                    'article_status' => self::STATUS_NORMAL
                ],
                'ORDER' => [
                    'sort',
                    'id'
                ]
            ]);
        if ($list && $format) {
            $aids = array_column($list, 'aid');
            //查询NOTE信息
            $articles = mArticle::getAllArticles($aids);
            //查询指南信息
            $guides = Guide::getGuideByIdsALl($aids);
            //查询文章信息
            $blank = Blank::getAllBlanks($aids);
            
            $notes = Photos::getNotesInArticleById($aids);
            
            $blank = empty($blank) ? [] : array_column($blank, null, 'bid');
            $articles = empty($articles) ? [] : array_column($articles, null, 'aid');

            //内容id分组
            $contentMap = [];
            foreach ($list as $key => $item) {
                $contentMap[$item['article_type']][] = $item['aid'];
            }
            foreach ($contentMap as $article_type => $aids) {
                $contentMap[$article_type] = AdmContent::get($article_type, $aids);
            }

            foreach ($list as $key => $item) {
                $aid = $item['aid'];
                $list[$key]['list_info'] = $contentMap[$item['article_type']][$item['aid']] ?? [];
                $list[$key]['article_type_desc'] = self::ADMIN_ARTICLE_TYPE_MAP[$item['article_type']] ?? '';
                switch ($item['article_type']) {
                    case self::ARTICLE_TYPE_HOUSE:
                        $list[$key]['house_info'] = [
                            'title' => isset($articles[$aid]) ? $articles[$aid]['title'] : '',
                            'cover_pic' => isset($articles[$aid]) && $articles[$aid]['cover_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $articles[$aid]['cover_pic_id'], ImgUrl::IMAGE_SIZE_SQ_PHONE) : '',
                        ];
                        break;
                    case self::ARTICLE_TYPE_GUIDE:
                        $list[$key]['house_info'] = [
                            'title' => isset($guides[$aid]) ? $guides[$aid]['title'] : '',
                            'cover_pic' => isset($guides[$aid]) && $guides[$aid]['cover_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $guides[$aid]['cover_pic_id'], ImgUrl::IMAGE_SIZE_SQ_PHONE) : '',
                        ];
                        break;
                    case self::ARTICLE_TYPE_TEXT:
                        $list[$key]['house_info'] = [
                            'title' => isset($blank[$aid]) ? $blank[$aid]['title'] : '',
                            'cover_pic' => isset($blank[$aid]) && $blank[$aid]['cover_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $blank[$aid]['cover_pic_id'], ImgUrl::IMAGE_SIZE_SQ_PHONE) : '',
                        ];
                        break;
                    case self::ARTICLE_TYPE_NOTE:
                        $list[$key]['house_info'] = [
                            'title' => isset($notes[$aid]) ? $notes[$aid]['title'] : '',
                            'cover_pic' => isset($notes[$aid]) && $notes[$aid]['cover_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $notes[$aid]['cover_pic_id'], ImgUrl::IMAGE_SIZE_SQ_PHONE) : '',
                        ];
                        break;
                }
            }
        }
        return $list;
    }

    public static function changeStatusById($id, $status)
    {
        if (self::update(['article_status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }
}